package com.dld.coupon.view;

import java.net.URLEncoder;

import android.graphics.Color;
import android.location.Location;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.constant.net.Constant;
import com.dld.android.view.MyHtmlTagHandler;
import com.dld.coupon.activity.R;
import com.dld.coupon.util.MapUtil;
import com.dld.coupon.util.StringUtils;
import com.dld.protocol.image.ImageProtocol;
import com.dld.protocol.json.BankCouponDetail;
import com.dld.protocol.json.CouponDetail;
import com.dld.protocol.json.GroupDetail;

public abstract class BaseDownLoadAdapter extends
		DownloadListView.DownLoadAdapter {
	protected View bankDetailToView(BankCouponDetail paramBankCouponDetail) {
		View localView = ((LayoutInflater) getContext().getSystemService(
				"layout_inflater")).inflate(R.layout.bank_item_layout, null);
		((TextView) localView.findViewById(R.id.coupon_name))
				.setText(paramBankCouponDetail.name);
		((TextView) localView.findViewById(R.id.coupon_address))
				.setText(paramBankCouponDetail.address);
		TextView localTextView = (TextView) localView
				.findViewById(R.id.coupon_distance_text);
		if ((paramBankCouponDetail.distance <= 0)
				|| (paramBankCouponDetail.distance > 5000))
			localTextView.setVisibility(8);
		else
			localTextView.setText(paramBankCouponDetail.distance + "米");
		localTextView = (TextView) localView.findViewById(R.id.coupon_tag);
		Object localObject = new StringBuilder();
		if (!StringUtils.isEmpty(paramBankCouponDetail.landmark))
			((StringBuilder) localObject)
					.append(paramBankCouponDetail.landmark);
		if (!StringUtils.isEmpty(paramBankCouponDetail.trade_name)) {
			if (((StringBuilder) localObject).length() != 0)
				((StringBuilder) localObject).append("/");
			((StringBuilder) localObject)
					.append(paramBankCouponDetail.trade_name);
		}
		localObject = ((StringBuilder) localObject).toString();
		if (!StringUtils.isEmpty((String) localObject))
			localTextView.setText((CharSequence) localObject);
		else
			localTextView.setVisibility(8);
		return (View) localView;
	}

	private View bindViewByCoupon(CouponDetail paramCouponDetail) {
		View localView = ((LayoutInflater) getContext().getSystemService(
				"layout_inflater")).inflate(R.layout.list_item_layout_coupon,
				null);

		if (paramCouponDetail.getImage() == null) {
			// ((ImageView) localView.findViewById(R.id.coupon_image))
			// .setVisibility(View.GONE);
		} else {
			ImageView img = (ImageView) localView
					.findViewById(R.id.coupon_image);
			ImageProtocol imp = new ImageProtocol(img.getContext(),
					paramCouponDetail.getImage());
			imp.startTrans(img);
		}

		if (paramCouponDetail.getDisid() == null) {
			((TextView) localView.findViewById(R.id.coupon_name))
					.setText(Html
							.fromHtml("<font color=#fff28100>["
									+ paramCouponDetail.getCatname() == null ? ""
									: paramCouponDetail.getCatname().trim()
											+ "]</font>"
											+ paramCouponDetail.getShopname() == null ? ""
											: paramCouponDetail.getShopname()
													.trim()));
		} else {
			((TextView) localView.findViewById(R.id.coupon_name)).setText(Html
					.fromHtml(paramCouponDetail.getTitle() == null ? ""
							: paramCouponDetail.getTitle().trim()));
		}
		if (paramCouponDetail.getCatname() != null) {
			((TextView) localView.findViewById(R.id.cat_name)).setText(Html
					.fromHtml(paramCouponDetail.getCatname() == null ? ""
							: paramCouponDetail.getCatname().trim()));
		}
		if (paramCouponDetail.getRank() != null) {
			float rating = Float.parseFloat(paramCouponDetail.getRank());
			RatingBar rb = (RatingBar) localView.findViewById(R.id.rating_bar);
			rb.setRating(rating);
		}
		if (paramCouponDetail.getDiscount() != null) {
			SpannableStringBuilder sb = new SpannableStringBuilder();
			sb.append("折扣");
			int s1 = sb.length();
			sb.append(":" + paramCouponDetail.getDiscount() == null ? ""
					: paramCouponDetail.getDiscount().trim() + "折");
			ForegroundColorSpan fcs = new ForegroundColorSpan(Color.RED);
			sb.setSpan(fcs, s1, sb.length(),
					SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
			TextView temp = (TextView) localView
					.findViewById(R.id.discount_content);
			temp.setText(sb);
		}
		TextView temp = (TextView) localView
				.findViewById(R.id.coupon_description);
		temp.setText(Html.fromHtml(paramCouponDetail.getDescription()));
		if (paramCouponDetail.getAddress() != null) {
			((TextView) localView.findViewById(R.id.coupon_address))
					.setText(paramCouponDetail.getAddress() == null ? ""
							: paramCouponDetail.getAddress().trim());
		}
		TextView localTextView = (TextView) localView
				.findViewById(R.id.coupon_distance_text);
		if ((paramCouponDetail.distance <= 0)
				|| (paramCouponDetail.distance > 5000))
			localTextView.setVisibility(8);
		else
			localTextView.setText(paramCouponDetail.distance + "米");
		localTextView = (TextView) localView.findViewById(R.id.coupon_tag);
		StringBuilder localStringBuilder = new StringBuilder();
		if (!StringUtils.isEmpty(paramCouponDetail.landmark)) {
			String str = paramCouponDetail.landmark.split(" ")[0];
			if (str.length() > 10)
				str = str.substring(0, 10) + "...";
			localStringBuilder.append(str);
		}
		if (!StringUtils.isEmpty(paramCouponDetail.trade_name)) {
			if (localStringBuilder.length() != 0)
				localStringBuilder.append("/");
			localStringBuilder.append(paramCouponDetail.trade_name);
		}
		String str = localStringBuilder.toString();
		if (!StringUtils.isEmpty(str))
			localTextView.setText(str);
		else
			localTextView.setVisibility(View.GONE);
		localView.setTag(paramCouponDetail);
		return localView;

	}

	private View bindViewByStore(CouponDetail couponDetail) {
		View localView = ((LayoutInflater) getContext().getSystemService(
				"layout_inflater")).inflate(R.layout.list_item_layout_store,
				null);
		if (couponDetail.getImage() == null) {
			// ((ImageView) localView.findViewById(R.id.coupon_image))
			// .setVisibility(View.GONE);
		} else {
			ImageView img = (ImageView) localView
					.findViewById(R.id.coupon_image);
			ImageProtocol ip = new ImageProtocol(img.getContext(), couponDetail
					.getImage());
			ip.startTrans(img);
		}
		TextView title = (TextView) localView.findViewById(R.id.store_tile);
		TextView address = (TextView) localView
				.findViewById(R.id.store_address);
		TextView cate = (TextView) localView.findViewById(R.id.category);

		title.setText(couponDetail.getShopname() == null ? "" : couponDetail
				.getShopname().trim());
		address.setText(couponDetail.getAddress() == null ? "" : couponDetail
				.getAddress().trim());
		cate.setText(couponDetail.getCatname() == null ? "" : couponDetail
				.getCatname().trim());

		return localView;
	}

	public double getDistance(double lat1, double lon1, double lat2, double lon2) {
		float[] results = new float[1];
		Location.distanceBetween(lat1, lon1, lat2, lon2, results);
		return results[0];
	}

	private View bindViewByStoreAround(CouponDetail cd) {

		View localView = ((LayoutInflater) getContext().getSystemService(
				"layout_inflater")).inflate(
				R.layout.list_item_layout_coupon_around, null);
		String cat = cd.getCatname();
		String title = cd.getShopname();
		String address = cd.getAddress();
		double lx = Double.parseDouble(cd.getX());
		double ly = Double.parseDouble(cd.getY());
		String discount = cd.getDiscount();
		String rank = cd.getRank();
		String dist = "";
		if (MapUtil.location != null) {
			double ds = getDistance(MapUtil.location.lat, MapUtil.location.lon,
					ly, lx);
			dist = String.format("%d", (int) ds);
		}
		TextView cat_tv = (TextView) localView
				.findViewById(R.id.cat_name_title);
		TextView address_tv = (TextView) localView
				.findViewById(R.id.coupon_address);
		RatingBar rank_tv = (RatingBar) localView.findViewById(R.id.rating_bar);
		TextView dis_tv = (TextView) localView.findViewById(R.id.discountid);
		TextView distance_tv = (TextView) localView.findViewById(R.id.distance);
		ForegroundColorSpan fs = new ForegroundColorSpan(Color
				.parseColor("#fff28100"));
		SpannableStringBuilder sb = new SpannableStringBuilder();
		sb.append("[" + cat == null ? "" : cat.trim() + "]");
		int l1 = sb.length();
		if (title != null) {
			sb.append(title == null ? "" : title.trim());
		}
		sb.setSpan(fs, 0, l1, SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
		cat_tv.setText(sb);
		address_tv.setText("区域:" + address == null ? "" : address.trim());
		dis_tv.setText(discount + "折");
		rank_tv.setRating(Float.parseFloat(rank));
		distance_tv.setText("" + dist + "米");
		return localView;
	}

	protected View couponDetailToView(CouponDetail paramCouponDetail,
			int view_type) {

		View rview = null;
		switch (view_type) {
		case Constant.VIEW_AROUND_STORE:
			rview = bindViewByStoreAround(paramCouponDetail);
			break;
		case Constant.VIEW_COUPON:
			rview = bindViewByCoupon(paramCouponDetail);
			break;
		case Constant.VIEW_STORE:
			rview = bindViewByStore(paramCouponDetail);
			break;
		default:
			break;
		}
		rview.setTag(paramCouponDetail);
		return rview;
	}

	protected View groupDetailToView(GroupDetail paramGroupDetail) {

		View localView = ((LayoutInflater) getContext().getSystemService(
				"layout_inflater")).inflate(R.layout.group_item_layout, null);
		Object localObject1 = (ImageView) localView
				.findViewById(R.id.coupon_image);
		Object localObject2 = (TextView) localView.findViewById(R.id.website);
		if (!StringUtils.isEmpty(paramGroupDetail.website))
			localObject1 = paramGroupDetail.website;
		else
			localObject1 = "其他";
		((TextView) localObject2).setText((CharSequence) localObject1);

		localObject2 = paramGroupDetail.image;
		if (localObject2 != null)
			new ImageProtocol(getContext(),
					"http://www.dld.com/tuan/viewimage?u="
							+ URLEncoder.encode((String) localObject2)
							+ "&w=200&h=200")
					.startTrans((ImageView) localObject1);
		((TextView) localView.findViewById(R.id.price))
				.setText(numberToString(paramGroupDetail.price));
		((TextView) localView.findViewById(R.id.value)).setText(Html.fromHtml(
				"<s>" + numberToString(paramGroupDetail.value) + "</s>", null,
				new MyHtmlTagHandler()));
		((TextView) localView.findViewById(R.id.bought))
				.setText(paramGroupDetail.bought);
		localObject1 = (TextView) localView.findViewById(R.id.distance);
		if ((paramGroupDetail.distance < 0)
				|| (paramGroupDetail.distance > 5000)) {
			if (!StringUtils.isEmpty(paramGroupDetail.range)) {
				if (paramGroupDetail.range.length() > 6)
					((TextView) localObject1).setText(paramGroupDetail.range
							.substring(0, 5)
							+ "...");
				else
					((TextView) localObject1).setText(paramGroupDetail.range);
			} else
				localView.findViewById(R.id.distance_image).setVisibility(
						View.GONE);
		} else
			((TextView) localObject1).setText(paramGroupDetail.distance + "米 ");
		((TextView) localView.findViewById(R.id.title)).setText(StringUtils
				.breakLines(paramGroupDetail.title));
		return (View) (View) localView;
	}

	protected String numberToString(double paramDouble) {
		String str = String.valueOf(paramDouble);
		if (str.endsWith(".0"))
			str = str.substring(0, -2 + str.length());
		return str;
	}
}
